package model

import (
	"time"

	"bmember/pkg/util"
)

const (
	ChannelMallMINA      int8 = 1             // 商城小程序
	ChannelBgMINA        int8 = 2             // 星团小程序
	ChannelMallByte      int8 = 3             // 抖音商城
	ChannelMallAlipay    int8 = 4             // 支付宝商城
	ChannelMallMinaStr        = "mall_mina"   // 商城微信小程序
	ChannelBgMinaStr          = "bg_mina"     // 星团小程序
	ChannelMallByteStr        = "mall_byte"   // 商城抖音小程序
	ChannelMallAlipayStr      = "mall_alipay" // 商城支付宝小程序
)

// LoginChannel 会员登录渠道
type LoginChannel struct {
	Id          int64     `json:"id" sql:"id"`
	BrandId     int64     `json:"brand_id" sql:"brand_id"`           // 品牌id
	MemberId    int64     `json:"member_id" sql:"member_id"`         // 用户id
	RegMemberId int64     `json:"reg_member_id" sql:"reg_member_id"` // 原始注册用户id
	OpenId      string    `json:"open_id" sql:"open_id"`             // 渠道识别用户id openid
	UnionId     string    `json:"union_id" sql:"union_id"`           // 用于识别跨渠道用户
	AppId       string    `json:"app_id" sql:"app_id"`               // 平台应用id
	Nickname    string    `json:"nickname" sql:"nickname"`           // 昵称
	Avatar      string    `json:"avatar" sql:"avatar"`               // 头像
	Channel     int8      `json:"channel" sql:"channel"`             // 渠道类型 1~商城小程序 2~星团小程序
	Ext         []byte    `json:"ext" sql:"ext"`                     // 渠道扩展信息, json
	CreatedAt   time.Time `json:"created_at" sql:"created_at"`
	UpdatedAt   time.Time `json:"updated_at" sql:"updated_at"`
	DeletedAt   int64     `json:"deleted_at" sql:"deleted_at"`
}

func NewLoginChannel(brandId, memberId int64, openId, appId, unionId, nickname, avatar string, channel int8) *LoginChannel {
	now := util.GetNow()
	return &LoginChannel{
		BrandId:     brandId,
		MemberId:    memberId,
		RegMemberId: memberId,
		OpenId:      openId,
		UnionId:     unionId,
		AppId:       appId,
		Nickname:    nickname,
		Avatar:      avatar,
		Channel:     channel,
		Ext:         []byte("{}"),
		CreatedAt:   now,
		UpdatedAt:   now,
	}
}

// GetInnerChannel channel 值转换
func GetInnerChannel(ch string) int8 {
	switch ch {
	case ChannelMallMinaStr:
		return ChannelMallMINA
	case ChannelBgMinaStr:
		return ChannelBgMINA
	case ChannelMallByteStr:
		return ChannelMallByte
	case ChannelMallAlipayStr:
		return ChannelMallAlipay
	default:
		return 0
	}
}

// SetChannel 设置渠道
func (l *LoginChannel) SetChannel(ch string) error {
	switch ch {
	case ChannelMallMinaStr:
		l.Channel = ChannelMallMINA
	case ChannelBgMinaStr:
		l.Channel = ChannelBgMINA
	case ChannelMallByteStr:
		l.Channel = ChannelMallByte
	case ChannelMallAlipayStr:
		l.Channel = ChannelMallAlipay
	default:
		return InvalidEnum
	}
	return nil
}

// GetChannel 获取渠道值
func (l *LoginChannel) GetChannel() string {
	switch l.Channel {
	case ChannelMallMINA:
		return ChannelMallMinaStr
	case ChannelBgMINA:
		return ChannelBgMinaStr
	case ChannelMallByte:
		return ChannelMallByteStr
	case ChannelMallAlipay:
		return ChannelMallAlipayStr
	default:
		return ""
	}
}

// IsSame 是否相同
func (l *LoginChannel) IsSame(brandId int64, openId, appId, channel string) bool {
	return l.BrandId == brandId && l.OpenId == openId && l.AppId == appId && l.Channel == GetInnerChannel(channel)
}
